package com.dh.basic;

import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.IntStream;

public class TestFibo {


    @Test
    public void test() {
        long start = System.currentTimeMillis();
        int n = 100;
        Assert.assertEquals(fibo(n), fibo2(n));
        System.out.println(System.currentTimeMillis() - start);
    }

    private long fibo(int n) {
        long array[] = new long[3];
        array[2] = 1;
        IntStream.range(0, n).forEach(i -> {
            if (i == 0 || i == 1) {
                array[0] = 1;
                array[1] = 1;
            } else {
                array[0] = array[1];
                array[1] = array[2];
                array[2] = array[0] + array[1];
            }
        });
        return array[2];
    }


    private long fibo2(int n) {
        long array[] = new long[3];
        array[2] = 1;
        IntStream.range(0, n).forEach(i -> {
            if (i == 0 || i == 1) {
                array[0] = 1;
                array[1] = 1;
            } else {
                array[2] = array[0] + array[1];
                array[0] = array[1];
                array[1] = array[2];
            }
        });
        return array[2];
    }

    @Test
    public void testMapKeyNull(){
        Map<String,String> map = new HashMap<>();
        map.put("4","3");
        map.put("3","2");
        map.put("2","1");
        map.put("5","3");
        System.out.println(getOriginKey(map,"4"));
        System.out.println(getOriginKey(map,"3"));
        String v = "6";
        while ((v = map.get(v)) != null) {
        }


        System.out.println("---"+v+"---");
    }

    public String getOriginKey(Map<String,String> map,String key){
        if(null == map.get(key)){
            return key;
        }
        return getOriginKey(map,map.get(key));
    }
}
